import numpy as np
import scipy.sparse as sparse
import scipy.io
import commands
import csv
import os
from itertools import izip 
import sys



def naiveTest(basefeature, targetfeature, targetcount, outfile):
	delimiter = ' # END OF FEATURE #\n'

	basef = open(basefeature, 'r')
	targetf = open(targetfeature, 'r')
	targetc = open(targetcount, 'r')

	basefeatures = basef.read().split(delimiter)[:-1]

	targetfeatures2count = dict()
	targetfeatures = targetf.read().split(delimiter)[:-1]
	# The first element is class label, not useful for test, ignore it.
	targetcount = np.array(targetc.read().split(',')[1:-1]).astype(int)
	for feature, count in izip(targetfeatures, targetcount):
		targetfeatures2count[feature] = count

	outf = csv.writer(open(outfile, 'w'))
	result = [targetfeatures2count[feature] if feature in targetfeatures2count else 0 for feature in basefeatures]
	outf.writerow(result)


if __name__=="__main__":
	naiveTest('/space/outputlogsTerry1000B1000M/featurelist_scaleHist_10_0.0001.log', '/space/test/Flat-Level.log', '/space/test/Flat-Level-Count.csv', '/space/test/FeatureVector.csv')

